package com.zs.java.generic;

/**
 * @author madison
 * @description
 * @date 2021/6/1 14:27
 */
public class Pecs {
    private class Plate<T> {
        private T item;

        public Plate() {
        }

        public Plate(T t) {
            item = t;
        }

        public void set(T t) {
            item = t;
        }

        public T get() {
            return item;
        }
    }

    //Lev 1
    class Food {
    }

    //Lev 2
    class Fruit extends Food {
    }

    class Meat extends Food {
    }

    //Lev 3
    class Apple extends Fruit {
    }

    class Banana extends Fruit {
    }

    class Pork extends Meat {
    }

    class Beef extends Meat {
    }

    //Lev 4
    class RedApple extends Apple {
    }

    class GreenApple extends Apple {
    }


    public void test() {
        //但实际上Java编译器不允许这个操作。会报错，“装苹果的盘子”无法转换成“装水果的盘子”。
//        Plate<Fruit> p = new Plate<>(new Apple());
        Plate<Fruit> p = new Plate<>(new Apple());
        System.out.println(p.get());

        //一、上届
        Plate<? extends Fruit> p1 = new Plate<>(new Apple());
        System.out.println(p1.get());
        //不能存入任何元素
//        p1.set(new Fruit());
//        p1.set(new Apple());
        Fruit fruit = p1.get();
        System.out.println("fruit:" + fruit);
        Object obj = p1.get();
        //error
//        Apple apple = p1.get();


        // 下届
        Plate<? super Fruit> p2 = new Plate<>(new Food());
        System.out.println(p2.get());
        //存入元素正常
        p2.set(new Fruit());
//        p2.set(new Food());
        p2.set(new Apple());
        p2.set(new Banana());
        //读取出来的东西只能存放在Object类里。
        Object object = p2.get();
        System.out.println(object);
    }

    public static void main(String[] args) {
        Pecs pecs = new Pecs();
        pecs.test();
    }
}
